package action.protocol;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import component.RunCmdComponent;
import constants.Constants;
import utils.GameUtils;

import java.io.File;

/**
 * @Auther syc.
 * @Date 2016/11/16 16:49.
 */
public class ProtocolEffectAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Application application = ApplicationManager.getApplication();
        RunCmdComponent component = application.getComponent(RunCmdComponent.class);

        //get the current project path
        String projectPath = GameUtils.getPropertyValue(anActionEvent, Constants.PROJECT_PATH_PRO_NAME);
        if (null == projectPath || projectPath.equals("")) {
            GameUtils.log("请配置工程路径：路径配置->工程路径");
            return;
        }

        String protocolPath = GameUtils.getPropertyValue(anActionEvent, Constants.PROTOCOL_PATH_PRO_NAME);
        if (null == protocolPath || protocolPath.equals("")) {
            GameUtils.log("请配置协议路径：路径配置->协议路径");
            return;
        }

        String oldPath = GameUtils.getPropertyValue(anActionEvent, Constants.PROTOCOL_EFFECT_PATH_PRO_NAME);

        if (null == oldPath || oldPath.equals("")) {
            String defaultEffectPath = projectPath + Constants.RELATIVE_EFFECT_PATH + File.separator + "msg";
            GameUtils.setGameProperties(anActionEvent, Constants.PROTOCOL_EFFECT_PATH_PRO_NAME, defaultEffectPath, "set the default effect path");
            GameUtils.log("协议生效路径默认为：" + defaultEffectPath + "\n如需请配置协议生效路径：消息协议->协议生效路径配置");
        }



        String protocolGenDirPath = File.separator + "gen" + File.separator + "server" + File.separator + "universal" + File.separator + "msg";

        String protocolEffectPath = GameUtils.getPropertyValue(anActionEvent, Constants.PROTOCOL_EFFECT_PATH_PRO_NAME);

        component.moveAndCopyFile(anActionEvent, "protocolEffect", protocolPath + protocolGenDirPath, protocolEffectPath);
    }

}
